import { cpuInfoAtom, memInfoAtom, netWorkLoadAtom } from '@/store'
import { FIXED_NUM, MEMORY_UNIT, NETWORK_UNIT } from '@shared/constants'
import { CpuMonitorData, MemMonitorData, MemStatus, NetworkMonitorData } from '@shared/models'
import { formatMemory } from '@shared/utils'
import { useAtomValue } from 'jotai'
import { useCallback, useEffect, useState } from 'react'

// CPU 监控数据缓存 hook
export const useCpuMonitorData: () => CpuMonitorData = () => {
  const cpuInfo = useAtomValue(cpuInfoAtom)
  const [cpuHistory, setCpuHistory] = useState<number[]>([])

  useEffect(() => {
    if (cpuInfo !== undefined) {
      // 保留 FIXED_NUM 位小数
      const newCpuInfo = Number(cpuInfo.toFixed(FIXED_NUM))
      setCpuHistory((prev) => {
        const newHistory = [...prev, newCpuInfo]
        // 保持最近10个数据点
        return newHistory.slice(-10)
      })
    }
  }, [cpuInfo])

  const clearHistory = useCallback(() => {
    setCpuHistory([])
  }, [])

  const newCpuInfo = cpuInfo !== undefined ? Number(cpuInfo.toFixed(FIXED_NUM)) : undefined

  return {
    currentValue: newCpuInfo,
    history: cpuHistory,
    clearHistory
  }
}

// 内存监控数据缓存 hook
export const useMemMonitorData: () => MemMonitorData = () => {
  const memInfo = useAtomValue(memInfoAtom)
  const [memHistory, setMemHistory] = useState<MemStatus[]>([])

  useEffect(() => {
    if (memInfo !== undefined) {
      // 使用 formatMemory 转换内存数据为 MEMORY_UNIT
      // 并保留 FIXED_NUM 位小数
      const formattedMemInfo: MemStatus = {
        used: Number(formatMemory(memInfo.used, MEMORY_UNIT).toFixed(FIXED_NUM)),
        total: Number(formatMemory(memInfo.total, MEMORY_UNIT).toFixed(FIXED_NUM)),
        usedPercent: Number(memInfo.usedPercent.toFixed(FIXED_NUM))
      }

      setMemHistory((prev) => {
        const newHistory = [...prev, formattedMemInfo]
        // 保持最近10个数据点
        return newHistory.slice(-10)
      })
    }
  }, [memInfo])

  const clearHistory = useCallback(() => {
    setMemHistory([])
  }, [])

  const newMemInfo =
    memInfo !== undefined
      ? {
          used: Number(formatMemory(memInfo.used, MEMORY_UNIT).toFixed(FIXED_NUM)),
          total: Number(formatMemory(memInfo.total, MEMORY_UNIT).toFixed(FIXED_NUM)),
          usedPercent: Number(memInfo.usedPercent.toFixed(FIXED_NUM))
        }
      : undefined

  return {
    currentValue: newMemInfo,
    history: memHistory,
    clearHistory
  }
}

// 网络负载监控数据缓存 hook
export const useNetworkMonitorData: () => NetworkMonitorData = () => {
  const networkLoad = useAtomValue(netWorkLoadAtom)
  const [networkHistory, setNetworkHistory] = useState<
    {
      rxRate: number
      txRate: number
    }[]
  >([])

  useEffect(() => {
    if (networkLoad !== undefined) {
      // 使用 formatMemory 转换网络流量数据为 NETWORK_UNIT
      // 并保留 FIXED_NUM 位小数
      const networkInfo: { rxRate: number; txRate: number } = {
        rxRate: Number(formatMemory(networkLoad.rxRate, NETWORK_UNIT).toFixed(FIXED_NUM)),
        txRate: Number(formatMemory(networkLoad.txRate, NETWORK_UNIT).toFixed(FIXED_NUM))
      }

      setNetworkHistory((prev) => {
        const newHistory = [...prev, networkInfo]
        // 保持最近30个数据点
        return newHistory.slice(-30)
      })
    }
  }, [networkLoad])

  const clearHistory = useCallback(() => {
    setNetworkHistory([])
  }, [])

  const newNetworkLoad =
    networkLoad !== undefined
      ? {
          rxRate: Number(formatMemory(networkLoad.rxRate, NETWORK_UNIT).toFixed(FIXED_NUM)),
          txRate: Number(formatMemory(networkLoad.txRate, NETWORK_UNIT).toFixed(FIXED_NUM))
        }
      : undefined

  return {
    currentValue: newNetworkLoad,
    history: networkHistory,
    clearHistory
  }
}
